#include "hellowidget.h"

widget_t *create_hello_widget(char* text, uint32_t width, uint32_t height)
{
    // create widget
    widget_t *self = create_widget(NULL,-1,-1,width,height);
    // set the application name
    hello_widget_set_text(self, text);
    // set the callback functions
    self->paint = hello_widget_paint;
    self->key_down = hello_widget_key_down;
    // return a pointer to the widget
    return self;
}

void hello_widget_set_text(widget_t *self, char *text)
{
    self->text = text;
    SendMessage(self->hwnd, WM_SETTEXT, (WPARAM)0, (LPARAM)text);
}

void hello_widget_paint(widget_t *self, HDC hdc)
{
    RECT rect;
    GetClientRect(self->hwnd, &rect);
    // draw the window edge
    DrawEdge(hdc, &rect, EDGE_RAISED, BF_RECT);
    // draw the text "hello world"
    SetBkMode(hdc, TRANSPARENT);
    DrawText(hdc, "Hello world! (press ESC to exit)", -1, &rect, 
             DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}

void hello_widget_key_down(widget_t *self, uint32_t vkey)
{
    // quit if the user press ESCAPE
    if(vkey == VK_ESCAPE)
    {
        PostQuitMessage(0);
    }
}

